package com.jsbb.util;

/**
 * Ascii
 *
 * @author w
 * @date 2021-03-05
 */
public class AsciiUtils {

    /**
     * ASCII转10进制字符串
     *
     * @param value
     */
    public static String asciiToDec(String value) {
        StringBuffer sb = new StringBuffer();
        for (byte b : value.getBytes()) {
            sb.append(b);
        }
        return sb.toString();
    }

    /**
     * 10进制字符串转ASCII
     *
     * @param value
     * @return
     */
    public static String decToAscii(String value) {
        int m;
        int n;
        int l = value.length() / 2;
        byte[] ret = new byte[l];
        for (int i = 0; i < l; i++) {
            m = i * 2 + 1;
            n = m + 1;
            ret[i] = Byte.valueOf(value.substring(i * 2, m) + value.substring(m, n));
        }
        return new String(ret);
    }

    /**
     * ASCII转10进制字符串
     *
     * @param v
     * @return
     */
    public static String ascii2Dec(String v) {
        StringBuilder sb = new StringBuilder();
        char[] ch = v.toCharArray();
        for (int i = 0; i < ch.length; i++) {
            sb.append(Integer.valueOf(ch[i]).intValue());
        }
        return sb.toString();
    }

    /**
     * 10进制字符串转ASCII
     *
     * @return
     */
    public static String dec2Ascii(String v) {
        return String.valueOf((char) Integer.parseInt(v));
    }
}
